package team.fivez.defaultmanage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.fivez.defaultmanage.dao.DefaultApplicationDao;
import team.fivez.defaultmanage.dao.RedoApplicationDao;
import team.fivez.defaultmanage.dao.RegionInfoDao;
import team.fivez.defaultmanage.domain.Classification;
import team.fivez.defaultmanage.domain.RegionInfo;
import team.fivez.defaultmanage.mediumDomain.FullClassification;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Component
public class RegionInfoService {

    private final DefaultApplicationDao defaultApplicationDao;

    private final RedoApplicationDao redoApplicationDao;

    private final RegionInfoDao regionInfoDao;

    @Autowired
    public RegionInfoService(DefaultApplicationDao defaultApplicationDao, RedoApplicationDao redoApplicationDao, RegionInfoDao regionInfoDao) {
        this.defaultApplicationDao = defaultApplicationDao;
        this.redoApplicationDao = redoApplicationDao;
        this.regionInfoDao = regionInfoDao;
    }

    //获取地域违约情况
    public List<FullClassification> getRegionDefaultSituation(){
        List<FullClassification> fullList = new ArrayList<>();
        regionInfoDao.deleteAllDefault();
        for (RegionInfo regionInfo : defaultApplicationDao.getDefaultRegionInfo()) {
            regionInfoDao.insertInfoToDefault(regionInfo.getRegion(),
                    String.valueOf(regionInfo.getApprovementTime().toLocalDateTime().getYear()));
        }

        List<String> yearList = regionInfoDao.getYearListFromDefault();
        List<String> regionList = regionInfoDao.getRegionListFromDefault();
        yearList.sort(Comparator.naturalOrder());

        for (String year : yearList) {
            List<Classification> classifyList = new ArrayList<>();
            for (String region : regionList) {
                Integer integer = regionInfoDao.getCountByRegionAndYearFromDefault(region, year);
                if (integer!=null){
                    classifyList.add(new Classification(region,integer));
                }
            }

            fullList.add(new FullClassification(year,classifyList));
        }

        return fullList;
    }

    //获取地域重生情况
    public List<FullClassification> getRegionRedoSituation(){
        List<FullClassification> fullList = new ArrayList<>();
        regionInfoDao.deleteAllRedo();
        for (RegionInfo regionInfo : redoApplicationDao.getRedoRegionInfo()) {
            regionInfoDao.insertInfoToRedo(regionInfo.getRegion(),
                    String.valueOf(regionInfo.getApprovementTime().toLocalDateTime().getYear()));
        }

        List<String> yearList = regionInfoDao.getYearListFromRedo();
        List<String> regionList = regionInfoDao.getRegionListFromRedo();
        yearList.sort(Comparator.naturalOrder());

        for (String year : yearList) {
            List<Classification> classifyList = new ArrayList<>();
            for (String region : regionList) {
                Integer integer = regionInfoDao.getCountByRegionAndYearFromRedo(region, year);
                if (integer!=null){
                    classifyList.add(new Classification(region,integer));
                }
            }

            fullList.add(new FullClassification(year,classifyList));
        }



        return fullList;

    }











}
